<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>扩展方法 | CatchAdmin 官网</title>
    <meta name="description" content="">
    <meta name="generator" content="VuePress 1.8.2">
    <style>a[title="站长统计"]{display:none}</style>
    <script data-ad-client="ca-pub-1505209242532150" async="true" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script src="https://v1.cnzz.com/z_stat.php?id=1279921342&amp;web_id=1279921342"></script>
    <meta name="description" content="基于 Thinkphp & Vue 开发的后台管理框架">
    <meta name="keywords" content="catchadmin, 后台管理, 前后端分离, thinkphp后台管理框架, thinkphp前后端分离框架,php, elementui">
    <link rel="preload" href="/assets/css/0.styles.6834c255.css" as="style"><link rel="preload" href="/assets/js/app.d0396f44.js" as="script"><link rel="preload" href="/assets/js/2.1cef47df.js" as="script"><link rel="preload" href="/assets/js/29.aae1a826.js" as="script"><link rel="prefetch" href="/assets/js/10.483f2013.js"><link rel="prefetch" href="/assets/js/11.981d4461.js"><link rel="prefetch" href="/assets/js/12.95d2c383.js"><link rel="prefetch" href="/assets/js/13.061c2fe0.js"><link rel="prefetch" href="/assets/js/14.ec2753db.js"><link rel="prefetch" href="/assets/js/15.8ba1c321.js"><link rel="prefetch" href="/assets/js/16.f7f0d34c.js"><link rel="prefetch" href="/assets/js/17.aac0bf62.js"><link rel="prefetch" href="/assets/js/18.2e2fe306.js"><link rel="prefetch" href="/assets/js/19.aee304b3.js"><link rel="prefetch" href="/assets/js/20.86a73bc1.js"><link rel="prefetch" href="/assets/js/21.8992c972.js"><link rel="prefetch" href="/assets/js/22.0a78500a.js"><link rel="prefetch" href="/assets/js/23.06342909.js"><link rel="prefetch" href="/assets/js/24.06fd96a3.js"><link rel="prefetch" href="/assets/js/25.00664d4e.js"><link rel="prefetch" href="/assets/js/26.c3d4b548.js"><link rel="prefetch" href="/assets/js/27.3c6927ad.js"><link rel="prefetch" href="/assets/js/28.74498c0b.js"><link rel="prefetch" href="/assets/js/3.4d2bdca7.js"><link rel="prefetch" href="/assets/js/30.c7732a2c.js"><link rel="prefetch" href="/assets/js/31.20403043.js"><link rel="prefetch" href="/assets/js/32.7b8e9cfe.js"><link rel="prefetch" href="/assets/js/33.8610da11.js"><link rel="prefetch" href="/assets/js/4.d7b1015b.js"><link rel="prefetch" href="/assets/js/5.ff87de2e.js"><link rel="prefetch" href="/assets/js/6.0e91e688.js"><link rel="prefetch" href="/assets/js/7.ba30e213.js"><link rel="prefetch" href="/assets/js/8.35122549.js"><link rel="prefetch" href="/assets/js/9.081b18e5.js">
    <link rel="stylesheet" href="/assets/css/0.styles.6834c255.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="ant-row"><div class="sidebar-button"><i aria-label="icon: bars" class="anticon anticon-bars"><svg viewBox="0 0 1024 1024" focusable="false" data-icon="bars" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M912 192H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM104 228a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0z"></path></svg></i> <span></span></div> <div class="ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-6 ant-col-lg-5 ant-col-xl-5 ant-col-xxl-4"><a href="/" class="router-link-active no-logo home-link"><!----> <span class="site-name">CatchAdmin 官网</span></a> <div class="search-box mobile-search"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div> <div class="ant-col ant-col-xs-0 ant-col-sm-0 ant-col-md-18 ant-col-lg-19 ant-col-xl-19 ant-col-xxl-20"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><ul role="menu" id="nav" class="ant-menu ant-menu-horizontal ant-menu-root ant-menu-light"><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/" class="router-link-active">
          🏠首页
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/docs/">
          📖文档
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item ant-menu-item-selected"><a href="/laravel/" class="router-link-active">
          📖Laravel
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/faq/">
          🤔FAQ
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/donate/">
          🎉赞助名单
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>
          🚀仓库地址
        </span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="visibility:hidden;position:absolute;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li></ul> <!----></nav></div></div> <!----></header> <aside class="sidebar"><div><div class="ads"><div id="ads_1"><img src="https://cover.kancloud.cn/akasishikelu/thinkphp6!middle"> <span title="基于 catchadmin 的 Thinkphp 6.0 企业级后台管理开发&amp;源码分析">基于 catchadmin 的 Thinkphp 6.0 企业级后台管理开发&amp;源码分析</span></div></div> <div role="separator" id="reset-margin" class="ant-divider ant-divider-horizontal ant-divider-dashed"></div></div> <ul class="sidebar-links"><li><a href="/laravel/" aria-current="page" title="介绍" class="sidebar-link">介绍</a></li><li><a href="/laravel/install.html" title="项目安装" class="sidebar-link">项目安装</a></li><li><a href="/laravel/module.html" title="模块" class="sidebar-link">模块</a></li><li><a href="/laravel/model.html" title="模型" class="sidebar-link">模型</a></li><li><a href="/laravel/console.html" title="命令" class="sidebar-link">命令</a></li><li><a href="/laravel/form.html" title="表单" class="sidebar-link">表单</a></li><li><a href="/laravel/table.html" title="表格组件" class="sidebar-link">表格组件</a></li><li><a href="/laravel/macros.html" aria-current="page" title="扩展方法" class="active sidebar-link">扩展方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/laravel/macros.html#扩展方法" title="扩展方法" class="sidebar-link">扩展方法</a></li><li class="sidebar-sub-header"><a href="/laravel/macros.html#query-扩展" title="Query 扩展" class="sidebar-link">Query 扩展</a></li><li class="sidebar-sub-header"><a href="/laravel/macros.html#集合扩展" title="集合扩展" class="sidebar-link">集合扩展</a></li><li class="sidebar-sub-header"><a href="/laravel/macros.html#migrate-扩展" title="migrate 扩展" class="sidebar-link">migrate 扩展</a></li></ul></li><li><a href="/laravel/excel.html" title="Excel" class="sidebar-link">Excel</a></li><li><a href="/laravel/issue.html" title="常见问题" class="sidebar-link">常见问题</a></li></ul></aside> <main class="page"> <div class="theme-antdocs-content content__default"><h2 id="扩展方法"><a href="#扩展方法" class="header-anchor">#</a> 扩展方法</h2> <p>关于 <code>Macros</code> 的信息，可以查询相关的 <code>Laravel</code> 文档。这里直接少 <code>CatchAdmin</code> 通过 <code>Macros</code> 提供了一些额外的方法。</p> <h2 id="query-扩展"><a href="#query-扩展" class="header-anchor">#</a> Query 扩展</h2> <p><code>builder</code> 的方法可以直接在模型中使用</p> <h4 id="wherelike-filed-value"><a href="#wherelike-filed-value" class="header-anchor">#</a> whereLike($filed, $value)</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token comment">// like 查询</span>
<span class="token variable">$model</span><span class="token operator">-&gt;</span><span class="token function">whereLike</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span>
</code></pre></div><h4 id="wheregt-filed-value"><a href="#wheregt-filed-value" class="header-anchor">#</a> whereGt($filed, $value)</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token comment">// 大于查询</span>
<span class="token variable">$model</span><span class="token operator">-&gt;</span><span class="token function">whereGt</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span>
</code></pre></div><h4 id="wherelt-filed-value"><a href="#wherelt-filed-value" class="header-anchor">#</a> whereLt($filed, $value)</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token comment">// 小于查询</span>
<span class="token variable">$model</span><span class="token operator">-&gt;</span><span class="token function">whereLt</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span>
</code></pre></div><h4 id="wherege-filed-value"><a href="#wherege-filed-value" class="header-anchor">#</a> whereGe($filed, $value)</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token comment">// 大于等于查询</span>
<span class="token variable">$model</span><span class="token operator">-&gt;</span><span class="token function">whereGe</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span>
</code></pre></div><h4 id="wherele-filed-value"><a href="#wherele-filed-value" class="header-anchor">#</a> whereLe($filed, $value)</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token comment">// 小于等于查询</span>
<span class="token variable">$model</span><span class="token operator">-&gt;</span><span class="token function">whereLe</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span>
</code></pre></div><h4 id="quicksearch"><a href="#quicksearch" class="header-anchor">#</a> quickSearch</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token comment">// 快速查找，结合 CatchTable 使用</span>
<span class="token variable">$model</span><span class="token operator">-&gt;</span><span class="token function">quickSearch</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span>
</code></pre></div><h4 id="tree"><a href="#tree" class="header-anchor">#</a> tree</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token comment">// 模型查询为 Tree 结构</span>
<span class="token variable">$model</span><span class="token operator">-&gt;</span><span class="token function">tree</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">,</span> <span class="token variable">$parentIds</span><span class="token punctuation">,</span> <span class="token operator">...</span><span class="token variable">$fields</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="集合扩展"><a href="#集合扩展" class="header-anchor">#</a> 集合扩展</h2> <h4 id="totree-int-pid-0-string-pidfield-parent-id-string-child-children"><a href="#totree-int-pid-0-string-pidfield-parent-id-string-child-children" class="header-anchor">#</a> <code>toTree</code>(int $pid = 0, string $pidField = 'parent_id', string $child = 'children')</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$collection</span><span class="token operator">-&gt;</span><span class="token function">toTree</span><span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$pid</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$pidField</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'parent_id'</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$child</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'children'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="export"><a href="#export" class="header-anchor">#</a> <code>export()</code></h4> <p>支持 Collection 导出到 excel 表格。</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$collection</span><span class="token operator">-&gt;</span><span class="token function">export</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$headers</span><span class="token punctuation">,</span> <span class="token keyword type-hint">bool</span> <span class="token variable">$csv</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="download"><a href="#download" class="header-anchor">#</a> <code>download()</code></h4> <p>支持 Collection 导出到 excel 表格并且下载。</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$collection</span><span class="token operator">-&gt;</span><span class="token function">download</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$headers</span><span class="token punctuation">,</span> <span class="token keyword type-hint">bool</span> <span class="token variable">$csv</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="migrate-扩展"><a href="#migrate-扩展" class="header-anchor">#</a> migrate 扩展</h2> <p>提供创建<code>migration</code>时候的一些便捷方法</p> <h4 id="创建-created-at"><a href="#创建-created-at" class="header-anchor">#</a> 创建 created_at</h4> <p>创建的是 unix timestamp，也就是 integer 类型</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$table</span><span class="token operator">-&gt;</span><span class="token function">createdAt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="创建-updated-at"><a href="#创建-updated-at" class="header-anchor">#</a> 创建 updated_at</h4> <p>创建的是 unix timestamp，也就是 integer 类型</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$table</span><span class="token operator">-&gt;</span><span class="token function">updatedAt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="创建-deleted-at"><a href="#创建-deleted-at" class="header-anchor">#</a> 创建 deleted_at</h4> <p>创建的是 unix timestamp，也就是 integer 类型</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$table</span><span class="token operator">-&gt;</span><span class="token function">deletedAt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="创建-unix-timestamp"><a href="#创建-unix-timestamp" class="header-anchor">#</a> 创建 unix timestamp</h4> <div class="language- extra-class"><pre class="language-text"><code>$table-&gt;unixTimestamp($softDeleted = true);
</code></pre></div><p>这个方式会创建 <code>created_at</code> <code>updated_at</code> 和 <code>deleted_at</code> 三个字段.</p> <p>如果不需要 <code>deleted_at</code>，</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$table</span><span class="token operator">-&gt;</span><span class="token function">unixTimestamp</span><span class="token punctuation">(</span><span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h4 id="创建人字段"><a href="#创建人字段" class="header-anchor">#</a> 创建人字段</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$table</span><span class="token operator">-&gt;</span><span class="token function">creatorId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>会创建类型为 <code>integer</code> 的 <code>creator_id</code> 字段, 默认值 <code>0</code></p> <h4 id="父级字段"><a href="#父级字段" class="header-anchor">#</a> 父级字段</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$table</span><span class="token operator">-&gt;</span><span class="token function">parentId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>会创建类型为 <code>integer</code> 的 <code>parent_id</code> 字段 , 默认值 <code>0</code></p> <h4 id="状态字段"><a href="#状态字段" class="header-anchor">#</a> 状态字段</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$table</span><span class="token operator">-&gt;</span><span class="token function">status</span><span class="token punctuation">(</span><span class="token variable">$default</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>会创建类型为 <code>tinyint</code> 的 <code>status</code> 字段, 默认值 <code>1</code></p> <h4 id="排序字段"><a href="#排序字段" class="header-anchor">#</a> 排序字段</h4> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$table</span><span class="token operator">-&gt;</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token variable">$default</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>会创建类型为 <code>integer</code> 的 <code>sort</code> 字段 , 默认值 <code>1</code></p></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/catch-admin/document/edit/master/docs/laravel/macros.md" target="_blank" rel="noopener noreferrer">在 Github 编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/laravel/table.html" class="prev"><i aria-label="icon: left" class="anticon anticon-left"><svg viewBox="64 64 896 896" focusable="false" data-icon="left" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8a31.86 31.86 0 0 0 0 50.3l450.8 352.1c5.3 4.1 12.9.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z"></path></svg></i>
        表格组件
      </a></span> <span class="next"><a href="/laravel/excel.html">
        Excel
        <i aria-label="icon: right" class="anticon anticon-right"><svg viewBox="64 64 896 896" focusable="false" data-icon="right" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 0 0 302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 0 0 0-50.4z"></path></svg></i></a></span></p></div> </main> <!----></div><div class="global-ui"></div></div>
    <script src="/assets/js/app.d0396f44.js" defer></script><script src="/assets/js/2.1cef47df.js" defer></script><script src="/assets/js/29.aae1a826.js" defer></script>
  </body>
</html>